package blackstar.performance.logger;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.logging.Logger;

import blackstar.performance.DaemonThreadFactory;

public class RollingFileLoggerFactory
{
	private final static Logger LOGGER = Logger
			.getLogger(RollingFileLoggerFactory.class.getSimpleName());
	private static List<RollingFileLogger> loggers = new ArrayList<RollingFileLogger>();
	private static ScheduledExecutorService scheduledExecutorService = Executors
			.newScheduledThreadPool(1, new DaemonThreadFactory());
	static
	{
		Runtime.getRuntime().addShutdownHook(new Thread()
		{
			public void run()
			{
				LOGGER.info("RollingFileLoggerFactory Shutdown");

				for (RollingFileLogger logger : loggers)
				{
					logger.close();
				}

				LOGGER.info("RollingFileLoggerFactory Shutdown Success");
			}
		});
	}

	public static synchronized RollingFileLogger create(String dirName,
			String fileName, int bufferSize)
	{
		RollingFileLogger logger = new RollingFileLogger(dirName, fileName,
				bufferSize, RollingFileLogger.ROLLING_TYPE_HOUR, scheduledExecutorService);
		loggers.add(logger);
		return logger;
	}
}
